<?php

namespace App\Http\Controllers;

use App\User;
use App\Util\Tool;
use http\Cookie;
use Illuminate\Http\Request;
use App\Models\Doctor;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;

class DoctorLoginController extends BaseController
{
    /**
     * 医护端登录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function Login(Request $request)
    {
        $phone = $request->phone ?? '';
        $password = $request->password ?? '';
        if (empty($phone)) {
            return $this->ResultJson(0, '账号不能为空', []);
        }
        //检查手机是否合法
        if (!Tool::checkMobile($phone)) {
            return $this->ResultJson(0, '手机号码错误', []);
        }
        if (empty($password)) {
            return $this->ResultJson(0, '密码不能为空', []);
        }
        //检查手机号是否存在
        $user = Doctor::where([['account', $phone],])->first();
        if (empty($user)) {
            return $this->ResultJson(0, '用户不存在请联系管理员', []);
        }
        //用户待审核
        if ($user->state == 0) {
            return $this->ResultJson(0, '该账号待审核,请联系管理员进行处理!', []);
        }
        //用户审核未通过
        if ($user->state == 2) {
            return $this->ResultJson(0, '该账号未通过审核 ,请联系管理员进行处理!', []);
        }
        //用户注销
        if ($user->state == 3) {
            return $this->ResultJson(0, '该账号已注销,请联系管理员进行恢复!', []);
        }
        //创建api_token
        $api_token = Tool::RandomToken("doctor_".$phone);
        if (Hash::check($password, $user->password) == false) {
            return $this->ResultJson(0, '用户名或者密码错误', []);
        }
        //设置登录临时标识
        Tool::SetDoctorLoginToken($phone, $api_token);
        //更新用户信息
        Doctor::where(['account' => $phone])->update([
            'api_token' => $api_token,
            'online' => 1,  //设置在线状态
            'updated_at' => now(),
        ]);
        //设置用户登录信息到redis
        Tool::SetDoctorAndUpToken($api_token, $phone);
        $username = 'doctor' . config("omnipay.Im.salt") . $user->id;
        $password = 'doctor' . config("omnipay.Im.salt") . $user->id;
        $tool['username'] = $username;
        $res = (new Easemob())->outLogin($tool);
        if($res==true){
            return $this->ResultJson(1, '验证成功', ['api_token' => $api_token, 'im_account' => $username, 'im_pass' => $password]);
        }
        return $this->ResultJson(0, '登录失败', ['api_token' => $api_token, 'im_account' => $username, 'im_pass' => $password]);

    }

    /** 密码重置
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function ResetPassword(Request $request)
    {
        if (empty($request->phone)) {
            return $this->ResultJson(0, '手机号码不能为空', []);
        }
        //检查手机号码是否正确
        if (!Tool::checkMobile($request->phone)) {
            return $this->ResultJson(0, '请填写正确的手号码', []);
        }
        //检查用户是否存在
        $user = Tool::IssetUser($request->phone);
        if (!$user) {
            return $this->ResultJson(0, '手机号未注册', []);
        }
        if (empty($request->code)) {
            return $this->ResultJson(0, '请输入短信验证码', []);
        }
        if (empty($request->password)) {
            return $this->ResultJson(0, '密码不能为空', []);
        }
        if (empty($request->password_confirmation)) {
            return $this->ResultJson(0, '确认密码不能为空', []);
        }
        if ($request->password != $request->password_confirmation) {
            return $this->ResultJson(0, '确认密码不一致', []);
        }
        //判断短信验证码是否过期
        $code = Tool:: GetSmsvalidator($request->phone);
        if (empty($code)) {
            return $this->ResultJson(0, '短信验证码已经过期', []);
        }
        //需要检查短信验证码是正确
        if ($request->code != $code) {
            return $this->ResultJson(0, '短信验证码错误', []);
        }

        //更新验证码为验证过了
        $this->SetCode($request->phone, $code);
        //生成token
        $api_token = Str::random(48) . md5($request->phone);
        //更新新密码
        $user->password = bcrypt($request->password);
        $user->api_token = $api_token;
        $user->save();
        //设置新token
        Tool::SetDoctorAndUpToken($api_token, $request->phone);
        return $this->ResultJson(1, '密码修改成功', ['api_token' => $api_token]);
    }

    /**
     * 获取用户登录信息
     * @return void
     */
    public function getUser(Request $request)
    {
        if (empty($request->api_token)) {
            return $this->ResultJson(0, 'api_token不能为空', []);
        }
        $result = Tool::getDoctorUser($request->api_token);
        if (empty($result)) {
            return $this->ResultJson(3, '用户信息不存在!', []);
        }
        return $this->ResultJson(1, '用户信息', $result);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 用户退出功能
     */
    public function UserDestroy(Request $request)
    {
        if (empty($request->api_token)) {
            return $this->ResultJson(0, 'api_token不能为空', []);
        }
        $user = Tool::ApiTokenGetDoctorUser($request->api_token);
        if (empty($user)) {
            return $this->ResultJson(3, '用户信息不存在！', []);
        }
        $user->online = 0;//设置离线
        $user->save();
        if (Tool::GetLoginToken($request->api_token)) {
            Redis::del($request->api_token);
            return $this->ResultJson(1, '退出成功', []);
        }
        return $this->ResultJson(0, '退出失败!', []);
    }
}
